package org.jeecg.modules.screen.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.jeecg.modules.screen.mapper.ScreenMapper;
import org.jeecg.modules.screen.service.IScreenService;
import org.jeecg.modules.screen.utils.DateUtil;
import org.openqa.selenium.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;


/**
 * @Description: 大屏信息
 * @Author: jeecg-boot
 * @Date: 2020-05-02
 * @Version: V1.0
 */
@Service
public class ScreenServiceImpl implements IScreenService {
    @Autowired
    ScreenMapper screenMapper;

    @Override
    public String queryBgDateState() {
        return screenMapper.queryBgDateState();
    }

    @Override
    public List<Map<String, Object>> queryKeywordTopRank() {
        List<Map<String, Object>> list = new ArrayList<>();
        String httpUrl = "http://api.tianapi.com/douyinhot/index?key=5b9a5bb416a1b05927abbfff337ee675";
        String jsonResult = request(httpUrl);
        JSONArray jsonArray = (JSONArray) JSON.parseObject(jsonResult).get("newslist");
        //获取屏蔽词
        List<String> blockWords = screenMapper.getBlockWords();
        int count  = 0;
        for(int i = 0;i<jsonArray.size();i++){

            if(count <= 5){
                boolean flag = true;
                for(String blockword:blockWords){
                    if(((JSONObject)jsonArray.get(i)).get("word").toString().contains(blockword)){
                        flag = false;
                    }
                }
                if(flag){
                    Map<String,Object> tempMap = new HashMap<>();
                    tempMap.put("name",((JSONObject)jsonArray.get(i)).get("word"));
                    Integer tempObj = (Integer) ((JSONObject)jsonArray.get(i)).get("hotindex");
                    tempMap.put("value",tempObj/100000);
                    tempMap.put("name",tempMap.get("name").toString().substring(0,4));
                    list.add(tempMap);
                    count++;
                }


            }else{
                break;
            }

        }
        return list;
    }

    public  String request(String httpUrl) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
//        httpUrl = httpUrl + "?" + httpArg;
//        httpUrl = httpUrl ;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> querySpiderDis() {
        return screenMapper.querySpiderDis();
    }

    @Override
    public List<Map<String, Object>> querySpiderArticle() {
        return screenMapper.querySpiderArticle();
    }

    @Override
    public Map<String, Object> queryTaskBrief() {

        Map<String, Object> result = new HashMap<>();
        // 获取当前任务数据
        Integer curTaskCount = screenMapper.getRutaskCount();
        result.put("current", curTaskCount);
        // 获取任务历史数据
        Integer hitaskCount = screenMapper.getHitaskCount();
        result.put("history", hitaskCount);
        //过期任务
        Integer overdue = screenMapper.getOdtaskCount();
        result.put("overdue", overdue);
        //总任务
        result.put("all", curTaskCount + hitaskCount);
        //待审核
        Integer waitReview = screenMapper.getWaitReviewCount();
        result.put("waitReview", waitReview);

        return result;
    }

    @Override
    public Map<String, Object> queryThirdPtFans() {
        List<String> nameList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        List<Map<String, Object>> basicList = screenMapper.queryThirdPtFans();
        for (int i = 0; i < basicList.size(); i++) {
            nameList.add(basicList.get(i).get("ptName").toString());
            dataList.add(Integer.parseInt(basicList.get(i).get("fansNum").toString()));
        }
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("ptName", nameList);
        retMap.put("fansNum", dataList);
        return retMap;
    }

    @Override
    public List<Map<String, Object>> queryThirdIdAndName() {
        return screenMapper.queryThirdIdAndName();

    }

    @Override
    public Map<String, Object> queryThirdViewAndForward(String id) {
        List<String> dateList = new ArrayList<>();
        List<Integer> viewList = new ArrayList<>();
        List<Integer> forwardList = new ArrayList<>();
        String startDate = DateUtil.getPastDate(30, "yyyyMMdd");
        List<Map<String, Object>> basicList = screenMapper.queryThirdViewAndForward(id, startDate);
        for (int i = 0; i < basicList.size(); i++) {
            dateList.add(basicList.get(i).get("date").toString());
            viewList.add(Integer.parseInt(basicList.get(i).get("viewNum").toString()));
            forwardList.add(Integer.parseInt(basicList.get(i).get("forwardNum").toString()));
        }
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("date", dateList);
        retMap.put("view", viewList);
        retMap.put("forward", forwardList);
        return retMap;
    }

    @Override
    public Integer queryActiveUserTotal() {
        String startTime = DateUtil.getPastDate(7,"yyyy-MM-dd hh:mm:ss");

        return screenMapper.queryActiveUserTotal(startTime);
    }

    @Override
    public Map<String, Object> queryActiveUserEveryDay() {
        List<String> dateList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        String startTime = DateUtil.getPastDate(7,"yyyy-MM-dd hh:mm:ss");

        List<Map<String, Object>> basicList = screenMapper.queryActiveUserEveryDay(startTime);
        for (int i = 0; i < basicList.size(); i++) {
            dateList.add(basicList.get(i).get("date").toString());
            valueList.add(Integer.parseInt(basicList.get(i).get("value").toString()));
        }
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("date", dateList);
        retMap.put("value", valueList);
        return retMap;
    }

    @Override
    public Map<String, Object> queryEveryDayViewZX() {
        List<String> dateList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        String startTime = DateUtil.getPastDate(7,"yyyy-MM-dd hh:mm:ss");

        List<Map<String, Object>> basicList = screenMapper.queryEveryDayViewZX(startTime);
        for (int i = 0; i < basicList.size(); i++) {
            dateList.add(basicList.get(i).get("date").toString());
            valueList.add(Integer.parseInt(basicList.get(i).get("value").toString()));
        }
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("date", dateList);
        retMap.put("value", valueList);
        return retMap;
    }

    @Override
    public Map<String, Object> queryZXBriefData() {
        String today = DateUtil.getPastDate(0, "yyyy-MM-dd");
        String yesterday = DateUtil.getPastDate(1, "yyyy-MM-dd");
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("today", today);
        queryMap.put("yesterday", yesterday);
        Map<String, Object> todayMap = screenMapper.queryZXBriefData(today);
        Map<String, Object> yesterdayMap = screenMapper.queryZXBriefData(yesterday);
        queryMap.clear();
        queryMap.putAll(todayMap);
        queryMap.put("uv_per",
                !todayMap.get("uv").toString().equals("0") ? (100 * Integer.parseInt(todayMap.get("uv").toString()) - Integer.parseInt(yesterdayMap.get("uv").toString())) / Integer.parseInt(todayMap.get("uv").toString()) : 0);
        queryMap.put("pv_per",
                !todayMap.get("pv").toString().equals("0") ? (100 * Integer.parseInt(todayMap.get("pv").toString()) - Integer.parseInt(yesterdayMap.get("pv").toString())) / Integer.parseInt(todayMap.get("pv").toString()) : 0);
        queryMap.put("vv_per",
                !todayMap.get("vv").toString().equals("0") ? (100 * Integer.parseInt(todayMap.get("vv").toString()) - Integer.parseInt(yesterdayMap.get("vv").toString())) / Integer.parseInt(todayMap.get("vv").toString()) : 0);
        queryMap.put("ip_per",
                !todayMap.get("ip").toString().equals("0") ? (100 * Integer.parseInt(todayMap.get("ip").toString()) - Integer.parseInt(yesterdayMap.get("ip").toString())) / Integer.parseInt(todayMap.get("ip").toString()) : 0);

        return queryMap;
    }

    @Override
    public List<Map<String, Object>> queryHotRemark() {
        return  screenMapper.queryHotRemark();
    }

    @Override
    public List<Map<String, Object>> queryOdTaskList() {
        return  screenMapper.queryOdTaskList();
    }

    @Override
    public List<Map<String, Object>> queryContentDataByType() {
        return  screenMapper.queryContentDataByType();
    }

    @Override
    public List<Map<String, Object>> queryJonList() {

        //查询用户信息
        List<Map<String, Object>> list = screenMapper.queryJonList();
        for(int i = 0 ; i <list.size();i++){

            List<Map<String, Object>> taskInfo = screenMapper.queryTaskListByUser(list.get(i).get("username").toString());
            if(null != taskInfo && taskInfo.size() > 0){
                list.get(i).put("taskInfo",taskInfo);
            }
        }
        return  list;
    }

    @Override
    public String getSwitch() {
        return  screenMapper.getSwitch();
    }

    @Override
    public List<Map<String, Object>> querySubmitRank() {
        return screenMapper.querySubmitRank();
    }

    @Override
    public List<Map<String, Object>> queryRefRank() {
        return screenMapper.queryRefRank();
    }
}
